-- 预留
local ImageButton = require "widgets/imagebutton"
local Widget = require "widgets/widget"

AddClassPostConstruct("widgets/redux/skilltreebuilder", function(self)
    local root = self.root
    if self.hx_btns then
        self.hx_btns:Kill()
    end
    self.hx_btns = root.xp:AddChild(Widget("hxbtns"))
    local btn_l = self.hx_btns:AddChild(ImageButton(GLOBAL.GetScrapbookIconAtlas("icon_cold.tex"), "icon_cold.tex"))
    local btn_r = self.hx_btns:AddChild(ImageButton(GLOBAL.GetScrapbookIconAtlas("icon_heat.tex"), "icon_heat.tex"))
    local w, h = root.xp_tospend:GetRegionSize()
    local size = 40
    local move_x = size + 2*w
    btn_l:SetPosition(-move_x, 0)
    btn_r:SetPosition(move_x, 0)
    
    local btn_size = btn_l:GetSize()
    local btn_scale = size/btn_size

    btn_l:ForceImageSize(size, size)
    btn_l:SetNormalScale(btn_scale)
    btn_l:SetFocusScale(btn_scale*1.2)
    btn_r:ForceImageSize(size, size)
    btn_r:SetNormalScale(btn_scale)
    btn_r:SetFocusScale(btn_scale*1.2)

    btn_l:SetHoverText("洗尽铅华, 重置洞察")
    btn_r:SetHoverText("醍醐灌顶, 洞察充盈")

    btn_l:SetOnClick(function()
        local prefab = self.target or (GLOBAL.ThePlayer and GLOBAL.ThePlayer.prefab)
        if not prefab then return end
        GLOBAL.TheSkillTree:RespecSkills(prefab)
        GLOBAL.TheFrontEnd:GetSound():PlaySound("wilson_rework/ui/respec")
        for skill,graphics in pairs(self.skillgraphics) do
            graphics.oldstatus ={}
            graphics.status = {}
        end
        local ui_anim = self.skilltreewidget and self.skilltreewidget.midlay and self.skilltreewidget.midlay.splash
        if ui_anim then
            -- self.skilltreewidget.fromfrontend = nil
            ui_anim:Kill()
            ui_anim = nil
        end
        self:RefreshTree()
        btn_l:SetHoverText("修改后的技能将会在新的存档生效！")
    end)
    btn_r:SetOnClick(function()
        local prefab = self.target or (GLOBAL.ThePlayer and GLOBAL.ThePlayer.prefab)
        if not prefab then return end
        local max_xp = GLOBAL.TheSkillTree:GetMaximumExperiencePoints()
        local xp = GLOBAL.TheSkillTree:GetSkillXP(prefab)
        local new_xp = max_xp - xp
        if new_xp > 0 then
            GLOBAL.TheFrontEnd:GetSound():PlaySound("wilson_rework/ui/respec")
            GLOBAL.TheSkillTree:AddSkillXP(new_xp, prefab)
            btn_r:SetHoverText("修改后的技能将会在新的存档生效！")
        elseif new_xp == 0 then
            btn_r:SetHoverText("已达到最大技能点, 无法修改!")
        end
        self:RefreshTree()
    end)
end)
